package com.bothin.experiment4.controller;

import com.bothin.experiment4.enums.ContactStatusEnum;
import com.bothin.experiment4.enums.LogistStatusEnum;
import com.bothin.experiment4.enums.ProductFormStatusEnum;
import com.bothin.experiment4.enums.ResultEnum;
import com.bothin.experiment4.model.LogistInfo;
import com.bothin.experiment4.model.ProductForm;
import com.bothin.experiment4.service.IContactDetailService;
import com.bothin.experiment4.service.IContactService;
import com.bothin.experiment4.service.ILogistInfoservice;
import com.bothin.experiment4.service.IProductFormService;
import com.bothin.experiment4.utils.ResultVOUtil;
import com.bothin.experiment4.vo.ResultVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by bothin
 * At 2019/12/19 20:58
 * Description:
 */
@Controller
@RequestMapping("/logist")
@Secured("ROLE_USER_STOCKMAN")
public class LogistInfoController {
    @Autowired
    private ILogistInfoservice logistInfoservice;
    @Autowired
    private IProductFormService productFormService;
    @Autowired
    private IContactDetailService contactDetailService;
    @Autowired
    private IContactService contactService;

    @RequestMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "8") Integer size,
                             @RequestParam(value = "keyword", defaultValue = "") String keyword,
                             Map<String, Object> map){
        PageHelper.startPage(page,size);
        List<LogistInfo> logistInfos = null;
        if (!StringUtils.isEmpty(keyword)){
            logistInfos = logistInfoservice.findByKeyword(keyword);
            map.put("key","&keyword="+keyword);
        }else {
            logistInfos = logistInfoservice.findAll();
        }
        PageInfo<LogistInfo> pageInfo = new PageInfo<>(logistInfos);
        map.put("pageInfo",pageInfo);
        return new ModelAndView("/stockman_manage/logist/list",map);
    }

    @RequestMapping("/change/status")
    @Transactional
    @ResponseBody
    public ResultVO change_status(@RequestParam("logistId") String logistId, @RequestParam("formId") String formId,
                                  @RequestParam("status") int status, HttpSession session){
        //改变物流状态
        int logistStatus = logistInfoservice.changeLogistStatus(logistId, status);
        //改变发货单状态
        int finshStatus = productFormService.changeFinshStatus(formId, status);

        //如果是完成状态 则判断合同是否履行完成
        if (status==ProductFormStatusEnum.FINISH.getCode()){
            ProductForm productForm = productFormService.findById(formId);
            if (contactDetailService.judgeAllPrintStatusIsZeroById(productForm.getContactId())&&
                productFormService.judgeAllFinshStatusByContactId(productForm.getContactId()))
                contactService.updatePerformanceStatusById(productForm.getContactId(), ContactStatusEnum.END_PERFORMANCE.getCode());
        }
        //操作成功
        if (logistStatus!=0 && finshStatus!=0){
            return ResultVOUtil.success();
        }
        return ResultVOUtil.error(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMessage());
    }


    @RequestMapping("to/detail")
    public ModelAndView to_detail(@RequestParam("logistId") String logistId , Map<String,Object> map){
        LogistInfo logistInfo = logistInfoservice.findById(logistId);
        map.put("logistInfo",logistInfo);

        return new ModelAndView("/stockman_manage/logist/detail",map);
    }

}
